// pages/contact/index.js
import {
	site
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		show: false,
		autosize: {
			minHeight: 100
		},
		fileList: [],
		loadMoreText: '加载中...', //底部文字
		list: [],
		realname: '',
		fileList2: [],
		content: '',
		site_id: '',
		happen_address: '',
		latitude: null,
		longitude: null,
		city: null,
		type: 'User',
		site_name: '',
		name:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getList(1)
		let type = wx.getStorageSync('type')
		if (type) {
			this.setData({
				type: type
			})
		} else {
			wx.showModal({
				title: '提示',
				content: '您的登录已过期,请重新登录~',
				showCancel: true, //是否显示取消按钮
				cancelText: "否", //默认是“取消”
				// cancelColor:'skyblue',//取消文字的颜色
				confirmText: "是", //默认是“确定”
				// confirmColor: 'skyblue',//确定文字的颜色
				success: function (res) {
					if (res.cancel) {
						//点击取消,默认隐藏弹框
						wx.navigateBack({
							delta: 1,
						})
					} else {
						//点击确定
						wx.navigateTo({
							url: '/pages/login/index',
						})
					}
				},
				fail: function (res) {}, //接口调用失败的回调函数
				complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
			})
		}


	},
	// 搜索
	onChange(e) {
		this.setData({
			name: e.detail
		})
		pageNum1 = 1
		this.setData({
			list: [],
		});
		this.getList(1)
	},
	// 获取列表
	getList(page) {
		var that = this
		site.query({
			page: page,
			size: 20,
			name:this.data.name
		}, res => {
			that.setData({
				total: res.data.total
			})
			if (res.data.list.length > 0) {
				that.setData({
					list: that.data.list.concat(res.data.list) //合并数据
				})
				if (11 > res.data.list.length > 0) {
					that.setData({
						loadMoreText: '' //没有更多了
					})
				}

			} else {
				if (page !== 1) {
					that.setData({
						loadMoreText: '暂无数据'
					})
				} else {
					that.setData({
						list: [],
						loadMoreText: '暂无数据'
					})
				}
			}
			wx.stopPullDownRefresh();
		})
	},
	toShow(e) {
		console.log(e)
		this.setData({
			show: true,
			site_id: e.currentTarget.dataset.id,
			site_name: e.currentTarget.dataset.name
		})
	},
	toCancel() {
		this.setData({
			show: false
		})
	},
	toCall(e) {
		let phone = e.currentTarget.dataset.phone
		wx.makePhoneCall({
			phoneNumber: phone,
		})
	},
	// 上传图片
	afterRead(event) {
		let that = this
		const {
			file
		} = event.detail;
		console.log(file)
		file.forEach(item => {
			wx.uploadFile({
				url: 'https://ncxpublic.ncscloudmedia.com/index.php/api/upload/image', // 仅为示例，非真实的接口地址
				filePath: item.path,
				name: 'file',
				formData: {
					user: 'test'
				},
				success(res) {
					console.log(res)
					const {
						fileList = [],
							fileList2 = []
					} = that.data;
					fileList.push({
						url: JSON.parse(res.data).data.url
					});
					fileList2.push(JSON.parse(res.data).data.path)
					console.log(fileList2)
					that.setData({
						fileList,
						fileList2,
					});
				},
			});
		})

	},
	deleteImg(event) {
		let index = event.detail.index
		let arr = this.data.fileList
		let arr2 = this.data.fileList2
		arr.splice(index, 1)
		arr2.splice(index, 1)
		this.setData({
			fileList: arr,
			fileList2: arr2
		})
	},
	// 发送
	toSubmit() {
		let obj = {
			site_id: this.data.site_id,
			content: this.data.content,
			images: this.data.fileList2.toString(),
			happen_address: this.data.happen_address,
		}
		let that = this
		wx.showModal({
			title: '提示',
			content: '确定要提交吗？',
			showCancel: true, //是否显示取消按钮
			cancelText: "否", //默认是“取消”
			// cancelColor:'skyblue',//取消文字的颜色
			confirmText: "是", //默认是“确定”
			// confirmColor: 'skyblue',//确定文字的颜色
			success: function (res) {
				if (res.cancel) {
					//点击取消,默认隐藏弹框

				} else {
					//点击确定
					site.message.create(obj, res => {
						if (res.code == 1) {
							wx.showToast({
								title: res.msg,
							})
							that.setData({
								show: false
							})
						} else {
							wx.showToast({
								title: res.msg,
								icon: 'error'
							})
						}
					})
				}
			},
			fail: function (res) {}, //接口调用失败的回调函数
			complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		pageNum1 = 1
		this.setData({
			list: [],
		});
		this.getList(1)
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {
		pageNum1 += 1;
		this.getList(pageNum1)
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})